﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net.Appender;
using Telerik.Web.UI;
using Aspose.Excel;

public partial class TeacherTrackerReport : BasePage
{
    public override string Icon
    {
        get
        {
            return "images/report_icon.png";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FilldropLstSelection();
        }
    }

    protected void lbExport_Click(object sender, ImageClickEventArgs e)
    {
      DataLayer dal = new DataLayer();
      string message = string.Empty;

      DataTable dt = null;

      string name = tbName.Text;
      decimal? sectionId = null;
      if (dropLstSection.SelectedIndex > 1)
      {
        sectionId = Decimal.Parse(dropLstSection.SelectedItem.Value);
      }
      dt = dal.GetTeacherTrackerReportData(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, name, sectionId, out message);

      int startRow = 1;
      string[] listFields = new string[] { "teachr_name", "course_name", "last_course_access", "total_student_count", 
        "total_teacher_online_time", "total_teacher_online_prev_week", "ungraded_item_count", 
        "first_date_accessed"};
      Excel ex = Utils.GridToExcel(dt, listFields, "Teacher Tracker", false, ref startRow);
      Worksheet ws = ex.Worksheets[0];

      for (int i = 0; i < dt.Rows.Count; ++i)
        for (int j = 0; j < listFields.Length; ++j)
        {
          ws.Cells[i + 1, j].Style.Borders[BorderType.LeftBorder].Color = Color.Black;
          ws.Cells[i + 1, j].Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i + 1, j].Style.Borders[BorderType.RightBorder].Color = Color.Black;
          ws.Cells[i + 1, j].Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i + 1, j].Style.Borders[BorderType.TopBorder].Color = Color.Black;
          ws.Cells[i + 1, j].Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i + 1, j].Style.Borders[BorderType.BottomBorder].Color = Color.Black;
          ws.Cells[i + 1, j].Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
        }
        
      ex.Save("Teacher Tracker.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Page.Response);
      Page.Response.End();
    }

    protected void rgTeacherTrackerReport_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        LoadData();
    }

    protected void btnGo_Click(object sender, EventArgs args)
    {
        LoadData();
        rgTeacherTrackerReport.Rebind();
    }

    /// <summary>
    /// Fill dropLstSelection
    /// </summary>
    private void FilldropLstSelection()
    {
        DataLayer dal = new DataLayer();
        string message;

        dropLstSection.Items.Clear();

        dropLstSection.DataSource = dal.GetSections(PAConfiguration.ConnectionString, out message);
        dropLstSection.DataValueField = "section_id";
        dropLstSection.DataTextField = "section_title";
        dropLstSection.DataBind();

        dropLstSection.Items.Insert(0, new ListItem("Select a section", "0"));
    }

    /// <summary>
    /// Fill rgBlastOffReport Data
    /// </summary>
    private void LoadData()
    {
        DataLayer dal = new DataLayer();
        string message;

        DataTable dt = null;

        string name = tbName.Text;

        decimal? sectionId = null;
        if (dropLstSection.SelectedIndex > 1)
        {
            sectionId = Decimal.Parse(dropLstSection.SelectedItem.Value);
        }

        dt = dal.GetTeacherTrackerReportData(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, name, sectionId, out message);

        if (dt == null)
        {
            rgTeacherTrackerReport.DataSource = null;
            rgTeacherTrackerReport.DataBind();
            return;
        }

        if (!string.IsNullOrEmpty(Sorting))
        {
            dt.DefaultView.Sort = Sorting;
        }

        rgTeacherTrackerReport.DataSource = dt;
    }


}